package com.example.minisqlite;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ResultSet {

    private final List<String> columns;
    private final List<Object[]> rows;
    private final boolean updateCount;
    private final int affectedRows;

    private ResultSet(List<String> columns, List<Object[]> rows, boolean updateCount, int affectedRows) {
        this.columns = columns;
        this.rows = rows;
        this.updateCount = updateCount;
        this.affectedRows = affectedRows;
    }

    public static ResultSet rows(List<String> columns, List<Object[]> rows) {
        return new ResultSet(new ArrayList<>(columns), new ArrayList<>(rows), false, -1);
    }

    public static ResultSet updateCount(int count) {
        return new ResultSet(Collections.emptyList(), Collections.emptyList(), true, count);
    }

    public boolean isUpdateCount() {
        return updateCount;
    }

    public int getAffectedRows() {
        if (!updateCount) {
            throw new IllegalStateException("Result is not an update count");
        }
        return affectedRows;
    }

    public List<String> getColumns() {
        if (updateCount) {
            throw new IllegalStateException("Result contains no rows");
        }
        return Collections.unmodifiableList(columns);
    }

    public List<Object[]> getRows() {
        if (updateCount) {
            throw new IllegalStateException("Result contains no rows");
        }
        return Collections.unmodifiableList(rows);
    }
}
